package com.spikything.ui {
	
	import flash.display.Sprite;
	import flash.events.Event;
	import com.irisnation.ing.formula1.ui.TextLabelMultiline;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import com.irisnation.ing.formula1.Style;
	import flash.text.TextFormat;
	import com.irisnation.ing.formula1.ui.ScrollBar;

	public class DynamicTextArea extends Sprite {
		
		public var content 		:Sprite;
		private var textArea 	:TextLabelMultiline;
		private var isHtml 		:Boolean;
		private var scroller 	:ScrollBar;
		
		public function DynamicTextArea (url:String, px:Number = 0, py:Number = 0, width:Number = 300, height:Number = 20, bgcolor:Number = 0, bgalpha:Number = .1, format:TextFormat = null, isHtml:Boolean = true) {
			x = px;
			y = py;
			this.isHtml = isHtml;
			textArea = new TextLabelMultiline(width, height, "", new TextFormat(Style.BODY_TEXT_FONT, 10, 0x000000), isHtml);
			textArea.background = true;
			textArea.backgroundColor = 0xf9f9f9;
			addChild(textArea);
						
			var textLoader:URLLoader = new URLLoader();
			textLoader.addEventListener(Event.COMPLETE, textLoaded);
			textLoader.load(new URLRequest(url));
			
			graphics.beginFill(bgcolor, bgalpha);
			graphics.drawRect(0, 0, width, height);
						
		}
		
		public function scrollTop ():void {
			textArea.scrollV = 0;
			scroller.scrollTop();
		}
		
		protected function scroll_text (n:Number):void {
			var target:TextField = textArea;
			target.scrollV = Math.round( ( target.maxScrollV - 1 ) * n ) + 1;
		}

		
		protected function textLoaded (e:Event):void {
			if (isHtml) {
				textArea.htmlText = e.target.data;
			} else {
				textArea.text = e.target.data;
			}
			textArea.scrollV = 0;
			if (textArea.maxScrollV>1) {
				scroller = new ScrollBar(scroll_text, textArea);
				addChild(scroller);
			}
		}
	
	}

}